use pc_keyboard::DecodedKey;
use crate::{print, println};
use crate::app::date::date;

const START_STR: &str = "blog_os> ";

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct Shell {
    skb_curr: usize,
    skb_buff: [char; 512]
}

impl Shell {
    /// Creates a new `Shell`.
    #[must_use]
    pub fn new() -> Shell {
        print!("{}", START_STR);
        Shell{
            skb_curr: 0,
            skb_buff: [' '; 512]
        }
    }

    pub fn input(&mut self, key: DecodedKey) {
        match key {
            DecodedKey::Unicode(character) => {
                if character == '\n' {
                    self.fire_command();
                    return;
                }

                self.skb_buff[self.skb_curr] = character;
                self.skb_curr+=1;
                print!("{}", character);
            }
            DecodedKey::RawKey(_) => {
               return; 
                
            }
        }
    }

    fn fire_command(&mut self) {
        let cmd= &self.skb_buff[0..self.skb_curr];
        match cmd {
            [] => {
                self.clear_line();
                println!();
            },
            ['d','a','t','e'] => {
                println!();
                date();
                println!();
                self.clear_line();
            },
            _ => {
                println!();
                println!("command {:?} not found", cmd);
                self.clear_line();
            }
        };
        print!("{}", START_STR);
    }

    fn clear_line(&mut self) {
        self.skb_curr = 0;
    }
}